/**
 * Huisou.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */

package com.chenzehe.java.core.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @description 操作配置文件工具类
 * @author chenzehe
 * @email hljuczh@163.com
 * @create 2012-9-20 上午08:38:48
 */

public class PropertiyUtil {
	protected static Logger	log	= LoggerFactory.getLogger(PropertiyUtil.class);
	
	/**
	 * 
	 * @param propertyName
	 * @param key
	 * @description 读取配置文件propertyName的属性key值，该配置文件存放在根目录下
	 * @author chenzehe
	 * @todo
	 */
	public static String getProperty(String propertyName, String key) {
		InputStream inputStream = PropertiyUtil.class.getClassLoader().getResourceAsStream(propertyName);
		Properties p = new Properties();
		String value = null;
		try {
			p.load(inputStream);
			value = p.getProperty(key);
			log.info("++++++++++++++++++++" + value);
		}
		catch (IOException e1) {
			e1.printStackTrace();
		}
		return value;
	}
	
	public static void setProperty(String propertyName, String key, String value) {
		try {
			String filePath = PropertiyUtil.class.getClassLoader().getResource(propertyName).getPath();
			// windows环境删除第一个/符
			if (System.getProperty("os.name").contains("Windows")) {
				filePath = filePath.substring(1);
			}
			OutputStream outputStream = new FileOutputStream(filePath);
			Properties p = new Properties();
			p.setProperty(key, value);
			p.store(outputStream, "save property " + propertyName);
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}
}
